package com.example.wangyi.mvp.ui.fragment.home;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.ColumnLayoutHelper;
import com.bumptech.glide.Glide;
import com.example.wangyi.R;
import com.example.wangyi.mvp.model.bean.HomeBean;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeBannerChannelAdapter extends DelegateAdapter.Adapter {
    private ColumnLayoutHelper gridLayoutHelper;
    private ArrayList<HomeBean.DataDTO.ChannelDTO> channel;

    public HomeBannerChannelAdapter(ColumnLayoutHelper gridLayoutHelper, ArrayList<HomeBean.DataDTO.ChannelDTO> channel) {
        this.gridLayoutHelper = gridLayoutHelper;
        this.channel = channel;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return gridLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new HomeChannelViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_channel, null, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        HomeChannelViewHolder channelViewHolder = (HomeChannelViewHolder) holder;
        HomeBean.DataDTO.ChannelDTO channelDTO = channel.get(position);
        channelViewHolder.channelTitle.setText(channelDTO.getName());
        Glide.with(channelViewHolder.channelImage).load(channelDTO.getIcon_url()).into(channelViewHolder.channelImage);
        channelViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ioClick != null){
                    ioClick.setIoClick(position);
                }
            }
        });
    }
    private IOClick ioClick;

    public void setIoClick(IOClick ioClick) {
        this.ioClick = ioClick;
    }

    public interface IOClick{
        void setIoClick(int page);
    }
    @Override
    public int getItemCount() {
        return channel.size();
    }

    static
    class HomeChannelViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.channel_image)
        ImageView channelImage;
        @BindView(R.id.channel_title)
        TextView channelTitle;

        HomeChannelViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
